import {createRouter,createWebHashHistory} from  'vue-router'
import easyapi from '../api'
const islogin=async function(to,from){
    
    //获取token
    let token=sessionStorage.getItem("easytoken");
    //验证token是否有效
    let result=await easyapi.get("/api/checktoken");
    if(!token || result.code!=200){
        return {path:'/login'};
    }
}
const router=createRouter(
    {
    history:createWebHashHistory(),
    routes:[
        {path:"/",redirect:"/easyb"},
        {path:'/easyb',
            component:()=>import("../views/easyb.vue"),
            children: [
                {
                    path: '/amusementfacility',
                    component: () => import("../views/amusementfacility.vue")
                },
                {
                    path: '/consumptionrecord',
                    component: () => import("../views/consumptionrecord.vue")
                },
                {
                    path: '/staff',
                    component: () => import("../views/staff.vue")
                },
                {
                    path: '/ticket',
                    component: () => import("../views/ticket.vue")
                }
                ,
                {
                    path: '/bill',
                    component: () => import("../views/bill.vue")
                }
                ,
                {
                    path: '/category',
                    component: () => import("../views/category.vue")
                }
                ,
                {
                    path: '/user',
                    component: () => import("../views/user.vue")
                }
                ,
                {
                    path: '/amusementtype',
                    component: () => import("../views/amusementtype.vue")
                }
                ,
                {
                    path: '/echarts',
                    component: () => import("../views/echarts.vue")
                }
            ],
            beforeEnter:islogin,
        },
        {path:'/login',
            component:()=>import("../views/login.vue")
        }
    ]
});

export default router;